// pages/to-pay-order/to-pay-order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNeedLogistics: 0,  // 需邮寄
    allGoodsPrice: 0, //商品总金额
    yunPrice: 0, //运费
    allGoodsAndYunPrice: 0,//商品总金额+运费
    orderType: "", //订单类型，购物车下单或立即支付下单，默认是购物车，
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    that.setData({
      isNeedLogistics: 1,  //显示收货地址标识
      orderType: e.orderType  //判断来自立即购买还是购物车
    });
  },

  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    var that = this;
    var shopList = [];

    //从立即购买跳转过来的
    if ("buyNow" == that.data.orderType) {
       var buyNowInfoMem = wx.getStorageSync('buyNowInfo');
       if (buyNowInfoMem && buyNowInfoMem.shopList) {
         shopList = buyNowInfoMem.shopList
        }
    }
    //从购物跳转过来的
     else { 
       var shopCarInfoMem = wx.getStorageSync('shopCarInfo');
       if (shopCarInfoMem && shopCarInfoMem.shopList) {
           shopList = shopCarInfoMem.shopList.filter(entity => {
           return entity.active;
        });
      }
    }
    that.setData({goodsList: shopList });
    that.initShippingAddress();
  },

  //初始化收货地址
  initShippingAddress: function () {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/tz/user/shipping-address/default',
      data: {
        token:""
      },
      success: (res) => {
        if (res.data.code == 0) {
          that.setData({curAddressData: res.data.data});
        } else {
          that.setData({curAddressData: null});
        }
      }
    })
  },

  //添加地址
  addAddress: function () {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },

  //选择地址
  selectAddress: function () {
    wx.navigateTo({
      url: "/pages/select-address/index"
    })
  },

  //提交订单
  createOrder: function (e) {
    wx.showLoading();
    var that = this;
    var loginToken = app.globalData.token // 用户登录 token
    var remark = ""; // 备注信息
    if (e) {
      remark = e.detail.value.remark; // 备注信息
    }

    var postData = {
      token: loginToken,
      goodsJsonStr: that.data.goodsJsonStr,
      remark: remark
    };

   //赋值收获地址信息
    if (that.data.isNeedLogistics > 0) {
      if (!that.data.curAddressData) {
        wx.hideLoading();
        wx.showModal({
          title: '错误',
          content: '请先设置您的收货地址！',
          showCancel: false
        })
        return;
      }
      postData.provinceId = that.data.curAddressData.provinceId;
      postData.cityId = that.data.curAddressData.cityId;
      if (that.data.curAddressData.districtId) {
        postData.districtId = that.data.curAddressData.districtId;
      }
      postData.address = that.data.curAddressData.address;
      postData.linkMan = that.data.curAddressData.linkMan;
      postData.mobile = that.data.curAddressData.mobile;
      postData.code = that.data.curAddressData.code;
    }

    if (!e) {
      postData.calculate = "true";
    }

   //生成订单
    wx.request({
      url: 'https://api.it120.cc/tz/order/create',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: postData, 
      success: (res) => {
        wx.hideLoading();
        if (res.data.code != 0) {
          wx.showModal({
            title: '错误',
            content: res.data.msg,
            showCancel: false
          })
          return;
        }

        if (e && "buyNow" != that.data.orderType) {
          // 清空购物车数据
          wx.removeStorageSync('shopCarInfo');
        }
        
        if (!e) {
          that.setData({
            isNeedLogistics: res.data.data.isNeedLogistics,
            allGoodsPrice: res.data.data.amountTotle,
            allGoodsAndYunPrice: res.data.data.amountLogistics + res.data.data.amountTotle,
            yunPrice: res.data.data.amountLogistics
          });
          return;
        }

        // 下单成功，跳转到订单管理界面
        wx.redirectTo({
          url: "/pages/order-list/index"
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})